package email;
/*
* 写在前面的话：
* 使用此代码需要导入两个jar包：
*   JavaMail mail.jar 1.4.5：             http://static.runoob.com/download/mail.jar
*   JAF（版本 1.1.1） activation.jar：     http://static.runoob.com/download/activation.jar
* 然后开启QQ邮箱授权码（）
* 在菜鸟教程有详细帮助的：https://www.runoob.com/java/java-sending-email.html*/


import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.GeneralSecurityException;
import java.util.Properties;
import java.util.Scanner;

//邮件主要逻辑类
class SendMessage{
    //邮件发送数量初始值
    private static int num=1;
    // 收件人电子邮箱
    public static String to ;

    // 发件人电子邮箱
    public static String from ;

    // 指定发送邮件的主机为 smtp.qq.com
    String host = "smtp.qq.com";  //QQ 邮件服务器

    //收件人邮箱集合
    //public Address address;


    public SendMessage(String to, String from) {
        this.to = to;
        this.from = from;
    }

    public SendMessage() {

    }


    public synchronized void sendEmailFun(){


        // 获取系统属性
        Properties properties = System.getProperties();

        // 设置邮件服务器
        properties.setProperty("mail.smtp.host", host);

        properties.put("mail.smtp.auth", "true");
        MailSSLSocketFactory sf = null;
        try {
            sf = new MailSSLSocketFactory();
        }
        catch (GeneralSecurityException e) {
            e.printStackTrace();
        }
        //assert sf != null;
        sf.setTrustAllHosts(true);
        properties.put("mail.smtp.ssl.enable", "true");
        properties.put("mail.smtp.ssl.socketFactory", sf);
        // 获取默认session对象
        Session session = Session.getDefaultInstance(properties,new Authenticator(){
            public PasswordAuthentication getPasswordAuthentication()
            {
                //发件人邮件用户名、密码（填入得到的授权码）
                return new PasswordAuthentication("2029518064@qq.com", "bvdmolrzfgikhgaf");
            }
        });

        try{
            // 创建默认的 MimeMessage 对象
            MimeMessage message = new MimeMessage(session);

            // Set From: 头部头字段
            message.setFrom(new InternetAddress(from));

            // Set To: 头部头字段
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            //message.addRecipient(Message.RecipientType.TO,address );

            // Set Subject: 头部头字段(邮件标题)
            message.setSubject("hello！");

            // 设置消息体（邮件内容）
            //message.setText("# \n"+"好久不见了，假期过得好吗？"+"第"+(num)+"封邮件发送完成！");
            message.setContent("<html lang=\"en\">\n" +
                            "<head>\n" +
                            "    <meta charset=\"UTF-8\">\n" +
                            "    <title>Title</title>\n" +
                            "</head>\n" +
                            "<body>\n" +
                            "<h1 >好好学习，天天向上！</h1>\n" +
                            "<h2></h2>\n" +
                            "<img src=\"https://image-upload-text.oss-cn-beijing.aliyuncs.com\n" +
                            "/blog_img/QQ%E5%9B%BE%E7%89%8720210301172030.gif\" alt=\"giao\" style=\"zoom:50%;\" />\n" +
                            "<p><br></p>\n" +
                            "<img src=\"https://image-upload-text.oss-cn-beijing.aliyuncs.com/blog_img/%E7%9C%BC%E7%A5%9E%E9%94%81%E5%AE%9A%E4%BD%A0.jpg\" alt=\"眼神锁定你\" style=\"zoom:150%;\" />\n" +
                            "<br>\n" +
                            "\n" +"第"+(num)+"封邮件发送完成！"+
                            "</body>\n" +
                            "</html>",
                    "text/html;charset=UTF-8" );

            // 发送消息
            Transport.send(message);
            System.out.println("第"+(num)+"封邮件发送完成！");
            num++;
        }catch (MessagingException mex) {
            mex.printStackTrace();
        }
    }
}
//线程类
class MyThread extends Thread{
    public static int emailCount;
    @Override
    public void run() {
        for (int i = 1; i <=emailCount; i++) {
            new SendMessage().sendEmailFun();
        }
    }
}
public class SendEmailThread {


    public static void main(String[] args) {
        //发件人地址
        String senderSite="2029518064@qq.com";
        //收件人地址
        String addresseeSite;

        Scanner reader=new Scanner(System.in);
        System.out.print("收件人QQ邮箱地址:");
        addresseeSite=reader.nextLine();
        new SendMessage(addresseeSite,senderSite);//收件人地址,发件人地址
        System.out.println("发送目标以确定！");
        System.out.print("发送次数：");

        /*------线程调度部分--------*/

        //发送邮件总数
        MyThread.emailCount= reader.nextInt();
        System.out.println("开始!");
        MyThread myThread1=new MyThread();
        myThread1.start();  //激活线程
        try{
            myThread1.join();
        }catch (InterruptedException ie){ }
        MyThread myThread2=new MyThread();
        try{
            myThread2.join();
        }catch (InterruptedException ie){}
        myThread2.start();
        System.out.println("程序运行结束！");
        System.exit(0);

    }
}
